<?php

/**
* AdminCSSSwitchTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';

class  AdminCSSSwitchTest extends WebTestCase
{
    private $url;
    private $testNAme = array();
    private $idArray = array();
    private $username = '';
    private $password = '';

    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url =$global['service_url'];
        $this->testName[] = md5(uniqid(rand(), true));
        $this->testName[] = md5(uniqid(rand(), true));
        $this->testName[] = md5(uniqid(rand(), true));

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  

  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $dbh->exec("INSERT INTO guide (name,owner) values ( '".$this->testName[0]."','".$this->username."' )");
        $this->idArray[$this->testName[0]] = $dbh->lastInsertId();

        $dbh->exec("INSERT INTO slide (guide_id,name,owner) values ( '".$this->idArray[$this->testName[0]]."','".md5(uniqid(rand(), true))."','".$this->username."' )");
        $dbh->exec("INSERT INTO slide (guide_id,name,owner) values ( '".$this->idArray[$this->testName[0]]."','".md5(uniqid(rand(), true))."','".$this->username."' )");
        $dbh->exec("INSERT INTO slide (guide_id,name,owner,video_file) values ( '".$this->idArray[$this->testName[0]]."','".md5(uniqid(rand(), true))."','".$this->username."','jNQXAC9IVRw' )");


        $dbh->exec("INSERT INTO guide (name,owner) values ( '".$this->testName[1]."','".$this->username."' )");
        $this->idArray[$this->testName[1]] = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide (name,owner) values ( '".$this->testName[2]."','".$this->username."' )");
        $this->idArray[$this->testName[2]] = $dbh->lastInsertId();

    }


    public function testCSSSelctionWireless()
    {

        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413");        

        $this->get($this->url.'?cmd=AdminGuideList');

        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");

        $this->get($this->url.'?cmd=AdminGuideList');
        

        $this->assertPattern('/title="wireless"/');

        
    }

    public function testCSSSelctionTouchscreen()
    {

        $this->addHeader("User-Agent: Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3 ");        
        $this->get($this->url.'?cmd=AdminGuideList');

        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");

        $this->get($this->url.'?cmd=AdminGuideList');

        $this->assertPattern('/title="touchscreen"/');
     
    }
    
    public function testCSSSelctionDesktop()
    {

        $this->addHeader("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");        
        $this->get($this->url.'?cmd=AdminGuideList');

        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");

        $this->get($this->url.'?cmd=AdminGuideList');

        $this->assertPattern('/title="desktop"/');
        
    }

    public function tearDown() 
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $dbh->exec("DELETE FROM slide WHERE guide_id = ".$this->idArray[$this->testName[0]]);
        $dbh->exec("DELETE FROM guide WHERE name LIKE '".$this->testName[0]."'" );
        $dbh->exec("DELETE FROM guide WHERE name LIKE '".$this->testName[1]."'" );
        $dbh->exec("DELETE FROM guide WHERE name LIKE '".$this->testName[2]."'" );

        AuthSingleton::instance()->removeUser($this->username);
        $this->get($this->url.'?cmd=AdminGuideList&logout=1');
        $dbh = null;
    }
    
}
?>

